* Set folder path here. Needs to contain subfolders "Data" and "Stata output"
cd "\\micro.intra\projekt\P0846$\P0846_Gem\carmagn\Coworker Peer effects in Voting\Accepted"

****************************************************************************
* This main do-file manage all data. Do-files must be run in the order 
* specified below due to dependencies.
*
* HPG = Homogenous Peer Group
* WP = Workplace 
****************************************************************************

* Global macro that defines WP HPG and family HPG, i.e., in addition to 
* belonging to the same WP or family, these characteristics are used to
* construct HPGs.
* The variables in the list must be categorical:
global HPG_Coworkers "EduCollege Female Occ1" 
global HPG_Relatives "EduCollege Female" 

******************************************************************************
* A. Basic data managment
******************************************************************************

* Make a dataset with ID:s of parents and grandparents of all individuals
do data_family_tree_2

* Neighbourhood codes of individuals
do data_DESO_Bostad17

* Firm info 2017
do data_firm_X_2017

* Individual characteristics of workers, siblings/cousins, and parents
do data_basic_ind_X
do data_ind_X_2017
do data_ind_X_2010
do data_parents_X

* Voting 2010, 2018, and 2022 as well as EU 2009
do data_voting_2010
do data_voting_2018
do data_voting_2022
do data_voting_EU_2009
do data_parents_voting

*****************************************************************************
* B. Determine relatives for all individuals using the family tree.
* For each indvidual, save a list of relatives
****************************************************************************
do data_relatives

***************************************************************************
* C. Determine coworkers. For each index worker, save a list of coworkers
* at the workplace
**************************************************************************
do data_coworkers
* Flag coworkers who has a relative which is a coworker at the workplace.
do data_coworkers_with_relatives_who_are_coworkers

**********************************************************************
* D. Identify relatives to be included, i.e., HPG in the main analysis
* but also other sets of relatives for alternative instruments
* Calculate averages of RHS variables of relatives for all coworkers
* These will then be aggregated to the workplace level
**********************************************************************
do data_relatives_X
do data_relatives_X_alt_instrument

***********************************************************************
* E. Finalize dataset for main analysis: Calculate averages of RHS 
* variables of coworkers, including the average of mean characteristics
* of relatives of coworkers, and merge with index workers and their
* characteristics.
**********************************************************************

* Workers 18-65 year old (main data)
do data_coworker_X_18_65_years

**********************************************************************
* F. Alternative instrument and placebo data
*********************************************************************
do data_coworker_X_18_65_years_alt_instrument
do data_placebo_coworker_X_18_65


